/**
 * 路由配置
 * @author Zeon scplay@gmail.com
 * @date 2017/8/2
 * @copyright Created by Panxsoft.
 */
import VueRouter from 'vue-router';
import iView from 'iview';
import Util from './libs/util';
import store from 'src/vuex/store';
import { getCookie } from './cookies';

const routers = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录首页'
        },
        component: function(resolve){

            return require.ensure([], () => resolve(require('./views/login/index.vue')), 'login');

        }

    },
    // admin权限下的路由
    {
        path: '/admin',
        meta: {
            title: '后台首页'
        },
        component: (resolve) => require.ensure([], () => resolve(require('./views/admin/index.vue')), 'admin-index'),

        children : [
            {
                path: 'home',
                meta: {
                    title: '后台首页'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/home/index.vue')), 'home')
            },
            {
                path: 'carousel',
                meta: {
                    title: '轮播图'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/carousel/views.vue')), 'carousel')
            },
            {
                path: 'video',
                meta: {
                    title: '视频专区'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/video/views.vue')), 'video')
            },
            {
                path: 'product',
                meta: {
                    title: '新增产品'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/product/views.vue')), 'product')
            },
            {
                path: 'product-category',
                meta: {
                    title: '产品分类'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/product/category/views.vue')), 'product-category')
            },
            {
                path: 'article-category',
                meta: {
                    title: '文章分类'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/article/category/views.vue')), 'article-category')
            },
            {
                path: 'article',
                meta: {
                    title: '新增文章'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/article/views.vue')), 'article')
            },
            {
                path: 'partner',
                meta: {
                    title: '合作伙伴'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/partner/views.vue')), 'partner')
            },
            {
                path: 'about',
                meta: {
                    title: '关于我们'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/admin/about/views.vue')), 'about')
            },
        ]
    },
    /**
     * 本地测试路由
     */
    {
        path: '/v-debug',
        children: [
            {
                path:'/',
                name: 'debug',
                meta: {
                    title: 'debug测试'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/debug/vuex.vue')), 'debug-vuex')
            },
            {
                path:'example',
                name: 'example',
                meta: {
                    title: 'example测试'
                },
                component: (resolve) => require.ensure([], () => resolve(require('./views/debug/example.vue')), 'example')
            },
        ]

    }
];

// 路由配置
const RouterConfig = {
    // mode: 'history',
    mode: 'hash',
    routes: routers
};

const router = new VueRouter(RouterConfig);

router.beforeEach(beforeEachRoute);
router.afterEach(afterEachRoute);

/**
 * 路由前件
 *
 * @param to
 * @param from
 * @param next
 */
function beforeEachRoute(to, from, next) {
    // iview show loading
    iView.LoadingBar.start();

    // reset title
    Util.title(to.meta.title);

    if(shouldLogOut(to)) return redirectToLoginPage(next);

    next();
}

/**
 * 如果不是登录页面，则判断是否存在用户的token，不然就重新跳转会login页面
 *
 * @param {Object} to
 * @return {boolean}
 */
function shouldLogOut(to) {
    return to.name !== 'login' && ! store.state.userToken.user_token
}

/**
 * 重定向到登录页面
 *
 * @param {Function} next
 */
function redirectToLoginPage(next) {

    iView.Message.error({
        content : '没有登录权限！即将为您跳转登录页面',
        duration : 3
    });

    setTimeout(function(){
        next({
            path : '/login'
        })
    },3000);
}

/**
 * 路由后件处理
 */
function afterEachRoute() {

    iView.LoadingBar.finish();

    window.scrollTo(0, 0);
}

export default router;
